package system_object_dao

import (
	"gitee.com/Luna-CY/Golang-Project-Template/internal/context"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/errors"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/interface/dao"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/logger"
	"gitee.com/Luna-CY/Golang-Project-Template/model"
)

func (cls *SystemObject) BatchTakeSystemObjectByCondition(ctx context.Context, options ...dao.SystemObjectOption) ([]*model.SystemObject, errors.Error) {
	if 0 == len(options) {
		logger.SugarLogger(logger.WithRequestId(ctx), logger.WithStack()).Errorf("IDSO_CT.SO_CT.BTSOBC_ON.130357 options is empty")

		return nil, errors.ErrorServerInternalError("IDSO_CT.SO_CT.BTSOBC_ON.150357", "异常的传入参数")
	}

	var session = cls.GetDb(ctx).Model(new(model.SystemObject))

	var metadata = dao.NewMetadata()
	for _, option := range options {
		var err errors.Error
		if session, err = option(session, metadata); nil != err {
			if err.IsType(errors.ErrorTypeRecordNotFound) {
				return nil, nil
			}

			return nil, err.Relation(errors.ErrorServerInternalError("IDSO_CT.SO_CT.BTSOBC_ON.280357", "配置选项失败"))
		}
	}

	var data []*model.SystemObject
	if err := session.Find(&data).Error; nil != err {
		logger.SugarLogger(logger.WithRequestId(ctx), logger.WithStack()).Errorf("IDSO_CT.SO_CT.BTSOBC_ON.340357 batch take by condition failed, err %v", err)

		return nil, errors.ErrorServerInternalError("IDSO_CT.SO_CT.BTSOBC_ON.360357", "批量获取数据失败")
	}

	return data, nil
}
